home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / QuickTime / Wired Movies and Sprites / DesktopSprites / QTSprites.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  13.1 KB  |  317 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        QTSprites.c
  3.     
  4.     Description: Contains code to create a sprite world and its associated sprites
  5.  
  6.     Author:        QuickTime team
  7.  
  8.     Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                 ("Apple") in consideration of your agreement to the following terms, and your
  12.                 use, installation, modification or redistribution of this Apple software
  13.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                 please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                 In consideration of your agreement to abide by the following terms, and subject
  17.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                 copyrights in this original Apple software (the "Apple Software"), to use,
  19.                 reproduce, modify and redistribute the Apple Software, with or without
  20.                 modifications, in source and/or binary forms; provided that if you redistribute
  21.                 the Apple Software in its entirety and without modifications, you must retain
  22.                 this notice and the following text and disclaimers in all such redistributions of
  23.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                 Apple Software without specific prior written permission from Apple.  Except as
  26.                 expressly stated in this notice, no other rights or licenses, express or implied,
  27.                 are granted by Apple herein, including but not limited to any patent rights that
  28.                 may be infringed by your derivative works or by other works in which the Apple
  29.                 Software may be incorporated.
  30.  
  31.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                 COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.                 
  45.     Change History (most recent first):
  46.  
  47.        <46>         08/30/00    srk        Carbonization
  48.  
  49. */
  50.  
  51.  
  52. /************************************************************
  53. *                                                                                                                                          *
  54. *    INCLUDE FILES                                                                                                             *
  55. *                                                                                                                                          *
  56. *************************************************************/
  57.  
  58. #ifndef _QTSPRITES_
  59. #include "QTSprites.h"
  60.  
  61.  
  62. static void QTSprites_CreateSprites (SpriteWorld theSpriteWorld);
  63.  
  64. /************************************************************
  65. *                                                                                                                                           *
  66. *    CONSTANTS                                                                                                                    *
  67. *                                                                                                                                           *
  68. *************************************************************/
  69.  
  70. enum {
  71.  
  72. numSprites                = 4,
  73. numSpaceShipImages        = 24,
  74. backgroundPictID        = 158,
  75. firstSpaceShipPictID    = (backgroundPictID + 1),
  76. spaceShipWidth            = 106,
  77. spaceShipHeight            = 80
  78.  
  79. };
  80.  
  81. /************************************************************
  82. *                                                                                                                                           *
  83. *    GLOBALS                                                                                                                         *
  84. *                                                                                                                                           *
  85. *************************************************************/
  86.  
  87. GWorldPtr                        gSpritePlane = NULL;
  88. Sprite                            gSprites[numSprites];
  89. Handle                            gCompressedPictures[numSpaceShipImages];
  90. ImageDescriptionHandle            gImageDescriptions[numSpaceShipImages];
  91. Rect                            gBounceBox;
  92. Rect                            gDestRects[numSprites];
  93. Point                            gDeltas[numSprites];
  94. short                            gCurrentImages[numSprites];
  95. Handle                            gCompressedPictures[numSpaceShipImages];
  96. RGBColor                        gBackgroundColor;
  97.  
  98. /************************************************************
  99. *                                                                                                                                           *
  100. *    QTSprites_CreateSpriteWorld()                                                                                  *
  101. *                                                                                                                                           *
  102. *    Creates our sprite world and its associated sprites                                                      *
  103. *                                                                                                                                           *
  104. *************************************************************/
  105.  
  106. SpriteWorld QTSprites_CreateSpriteWorld (Rect *windowBounds, WindowPtr windowPtr)
  107. {
  108.     OSErr err;
  109.     Rect bounds;
  110.     SpriteWorld theSpriteWorld = NULL;
  111.  
  112.     // calculate the size of the destination
  113.     bounds = *windowBounds;
  114.     MacOffsetRect (&bounds, -1 * bounds.left, -1 * bounds.top);
  115.     gBounceBox = bounds;
  116.     MacInsetRect (&gBounceBox, 16, 16);
  117.  
  118.     // create a sprite layer graphics world 
  119.     err = NewGWorld (&gSpritePlane, 0, &bounds, nil, nil, 0);
  120.     if (gSpritePlane == NULL)
  121.     {
  122.         NewGWorld (&gSpritePlane, 0, &bounds, nil, nil, 0);
  123.     }
  124.     
  125.     if (gSpritePlane)
  126.     {
  127.  
  128.         LockPixels (GetPortPixMap(gSpritePlane));
  129.         
  130.         gBackgroundColor.red = 0x1234;
  131.         gBackgroundColor.green = 0x0001;
  132.         gBackgroundColor.blue = 0x0044;
  133.         
  134.         // create a sprite world
  135.         err = NewSpriteWorld (&theSpriteWorld,        /* on return, a new sprite world */
  136.                                 (GWorldPtr)windowPtr,            /* destination */
  137.                                 gSpritePlane,        /* sprite layer graphics world */
  138.                                 &gBackgroundColor,    /* background color */
  139.                                 nil);                /* graphics world to be used as the background. */
  140.         
  141.         // create sprites
  142.         QTSprites_CreateSprites(theSpriteWorld);
  143.     }
  144.  
  145.     return theSpriteWorld;
  146. }
  147.  
  148. /************************************************************
  149. *                                                                                                                                           *
  150. *    QTSprites_CreateSprites()                                                                                          *
  151. *                                                                                                                                           *
  152. *    Creates the sprites for use with our sprite world                                                        *
  153. *                                                                                                                                           *
  154. *************************************************************/
  155.  
  156. void QTSprites_CreateSprites (SpriteWorld theSpriteWorld)
  157. {
  158.     short                lIndex;
  159.     Handle                hCompressedData = NULL;
  160.     PicHandle            hpicImage;
  161.     OSErr                nErr;
  162.     RGBColor            rgbcKeyColor;
  163.     
  164.     MacSetRect(&gDestRects[0], 132, 132, 132 + spaceShipWidth, 
  165.         132 + spaceShipHeight);
  166.     MacSetRect(&gDestRects[1], 50, 50, 50 + spaceShipWidth, 
  167.         50 + spaceShipHeight);
  168.     MacSetRect(&gDestRects[2], 100, 100, 100 + spaceShipWidth, 
  169.         100 + spaceShipHeight);
  170.     MacSetRect(&gDestRects[3], 130, 130, 130 + spaceShipWidth, 
  171.         130 + spaceShipHeight);
  172.  
  173.     gDeltas[0].h = -3;
  174.     gDeltas[0].v = 0;
  175.     gDeltas[1].h = -5;
  176.     gDeltas[1].v = 3;
  177.     gDeltas[2].h = 4;
  178.     gDeltas[2].v = -6;
  179.     gDeltas[3].h = 6;
  180.     gDeltas[3].v = 4;
  181.     
  182.     gCurrentImages[0] = 0;
  183.     gCurrentImages[1] = numSpaceShipImages / 4;
  184.     gCurrentImages[2] = numSpaceShipImages / 2;
  185.     gCurrentImages[3] = numSpaceShipImages * 4 / 3;
  186.     
  187.     rgbcKeyColor.red = 0;
  188.     rgbcKeyColor.green = 0;
  189.     rgbcKeyColor.blue = 0;
  190.     
  191.     // recompress PICT images to make them transparent
  192.     for (lIndex = 0; lIndex < numSpaceShipImages; lIndex++) 
  193.     {
  194.         ImageDescriptionHandle idh=nil;
  195.         Handle  imageData=nil;
  196.  
  197.         hpicImage = (PicHandle)GetPicture(lIndex +
  198.                                             firstSpaceShipPictID);
  199.         DetachResource((Handle)hpicImage);
  200.  
  201.         nErr = RecompressPictureWithTransparency(hpicImage,
  202.                                                 &rgbcKeyColor, 
  203.                                                 nil,
  204.                                                 &gImageDescriptions[lIndex],
  205.                                                 &gCompressedPictures[lIndex]);
  206.         KillPicture(hpicImage);
  207.     }
  208.  
  209.     // create the sprites for the sprite world
  210.     for (lIndex = 0; lIndex < numSprites; lIndex++) {
  211.         MatrixRecord    matrix;
  212.  
  213.         SetIdentityMatrix(&matrix);
  214.         
  215.         matrix.matrix[2][0] = ((long)gDestRects[lIndex].left << 16);
  216.         matrix.matrix[2][1] = ((long)gDestRects[lIndex].top << 16);
  217.  
  218.         nErr = NewSprite(&(gSprites[lIndex]),            /* on return, the ID of the new sprite */
  219.                         theSpriteWorld,                    /* the sprite world for this sprite */
  220.                         gImageDescriptions[lIndex],        /* image description of the sprite’s image. */
  221.                         *gCompressedPictures[lIndex],     /* sprite image data */
  222.                         &matrix,                        /* sprite matrix */
  223.                         true,                            /* is sprite visible? */
  224.                         lIndex);                         /* sprite layer */
  225.  
  226.     }
  227. }
  228.  
  229. /************************************************************
  230. *                                                                                                                                           *
  231. *    QTSprites_DisposeSpriteWorld()                                                                                *
  232. *                                                                                                                                           *
  233. *    Disposes our sprite world and its associated sprites                                                    *
  234. *                                                                                                                                           *
  235. *************************************************************/
  236.  
  237. void QTSprites_DisposeSpriteWorld (SpriteWorld theSpriteWorld)
  238. {
  239.     short    nIndex;
  240.  
  241.     // dispose of each sprite’s image data
  242.     for (nIndex = 0; nIndex < numSprites; nIndex++)
  243.     {
  244.         if (gSprites[nIndex])
  245.             DisposeSprite(gSprites[nIndex]);
  246.  
  247.         if (gCompressedPictures[nIndex])
  248.             DisposeHandle(gCompressedPictures[nIndex]);
  249.             
  250.         if (gImageDescriptions[nIndex])
  251.             DisposeHandle((Handle)gImageDescriptions[nIndex]);
  252.     }
  253.     
  254.     // dispose of the sprite plane world
  255.     if (gSpritePlane)
  256.         DisposeGWorld(gSpritePlane);
  257.  
  258.     // dispose of the sprite world and associated graphics world
  259.     if (theSpriteWorld)
  260.         DisposeSpriteWorld(theSpriteWorld);
  261. }
  262.  
  263.  
  264. /************************************************************
  265. *                                                                                                                                           *
  266. *    QTSprites_MoveSprites()                                                                                            *
  267. *                                                                                                                                           *
  268. *    Animate the sprites by adjusting their properties via                                                 *
  269. *     the SetSpriteProperty function                                                     *
  270. *                                                                                                                                           *
  271. *************************************************************/
  272.  
  273. void QTSprites_MoveSprites (void)
  274. {
  275.     short                nIndex;
  276.     MatrixRecord        matrix;
  277.  
  278.     SetIdentityMatrix(&matrix);
  279.  
  280.     // for each sprite
  281.     for (nIndex = 0; nIndex < numSprites; nIndex++) {
  282.     
  283.         // modify the sprite’s matrix
  284.         MacOffsetRect(&gDestRects[nIndex], gDeltas[nIndex].h,
  285.                     gDeltas[nIndex].v);
  286.         
  287.         if ((gDestRects[nIndex].right >= gBounceBox.right) ||
  288.             (gDestRects[nIndex].left <= gBounceBox.left))
  289.             gDeltas[nIndex].h = -gDeltas[nIndex].h;
  290.         
  291.         if ((gDestRects[nIndex].bottom >= gBounceBox.bottom) ||
  292.             (gDestRects[nIndex].top <= gBounceBox.top))
  293.             gDeltas[nIndex].v = -gDeltas[nIndex].v;
  294.         
  295.         matrix.matrix[2][0] = ((long)gDestRects[nIndex].left << 16);
  296.         matrix.matrix[2][1] = ((long)gDestRects[nIndex].top << 16);
  297.         
  298.         SetSpriteProperty(gSprites[nIndex],            /* the sprite for this operation */
  299.                             kSpritePropertyMatrix,    /* the property to be set */
  300.                             &matrix);                /* the new value for the property */
  301.         
  302.         // change the sprite’s image
  303.         gCurrentImages[nIndex]++;
  304.         if (gCurrentImages[nIndex] >= (numSpaceShipImages *
  305.                                         (nIndex+1)))
  306.         {
  307.             gCurrentImages[nIndex] = 0;
  308.         }
  309.         
  310.         SetSpriteProperty(gSprites[nIndex],
  311.                         kSpritePropertyImageDataPtr,
  312.                         *gCompressedPictures[gCurrentImages[nIndex] / (nIndex+1)]);
  313.     }
  314. }
  315.  
  316. #endif
  317.